package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿控制层
 * @author Xiao
 * @date 2021/9/29
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    /**
     * 查询所有地址数据
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> addressListApi() {
        List<AddressBook> addressBooks = addressBookService.addressListApi();
        return R.success(addressBooks);
    }
    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> addAddressApi(@RequestBody AddressBook addressBook) {
        addressBookService.addAddressApi(addressBook);
        return R.success("添加成功！");
    }
    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddressApi() {
        AddressBook defaultAddressBook = addressBookService.getDefaultAddressApi();
        if (defaultAddressBook != null) {
            log.info("defaultAddressBook:{}", defaultAddressBook.getConsignee());
            return R.success(defaultAddressBook);
        }
        return R.error("请添加地址！");
    }
    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> setDefaultAddressApi(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook.getId());
        addressBookService.setDefaultAddressApi(addressBook);
        return R.success("设置成功！");
    }
    /**
     * 根据id查询单个地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> addressFindOneApi(@PathVariable Long id) {
        log.info("id:{}", id);
        AddressBook addressBook = addressBookService.addressFindOneApi(id);
        if (addressBook == null) {
            return R.error("没有找到对应地址");
        }
        return R.success(addressBook);
    }
    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddressApi(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        addressBookService.updateAddressApi(addressBook);
        return R.success("修改成功！");
    }
    /**
     * 根据id删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAddressApi(Long ids) {
        log.info("ids:{}", ids);
        addressBookService.deleteAddressApi(ids);
        return R.success("删除成功！");
    }
    /**
     * 获取最新地址
     * @return
     */
    @GetMapping("/lastUpdate")
    public R<AddressBook> addressLastUpdateApi() {
        AddressBook lastUpdateAddressBook = addressBookService.addressLastUpdateApi();
        log.info("lastUpdateAddressBook:{}", lastUpdateAddressBook);
        return R.success(lastUpdateAddressBook);
    }
}
